/**
 *  Copyright [2011] Steffen Kämpke
 *  mailto: steffen.kaempke@pennychecker.de
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pennychecker.example.mvp.view;

import com.pennychecker.example.mvp.model.User;
import com.pennychecker.example.mvp.presenter.EditUserPresenter;
import com.pennychecker.swing.presenter.ext.event.HasClickHandlers;
import javax.swing.JComponent;
import javax.swing.JOptionPane;

/**
 *
 * @author Steffen Kämpke mailto:steffen.kaempke@pennychecker.de
 */
public class EditUserView extends javax.swing.JPanel implements EditUserPresenter.Display {

    private static final long serialVersionUID = 1L;

    /** Creates new form EditUserView */
    public EditUserView() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        textfieldFirstname = new com.pennychecker.swing.presenter.ext.component.STextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        textfieldLastname = new com.pennychecker.swing.presenter.ext.component.STextField();
        buttonSave = new com.pennychecker.swing.presenter.ext.component.SButton();
        buttonClose = new com.pennychecker.swing.presenter.ext.component.SButton();

        setName("User View"); // NOI18N
        setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.ipadx = 188;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 10, 0, 10);
        add(textfieldFirstname, gridBagConstraints);

        jLabel1.setText("Firstname");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 10, 0, 0);
        add(jLabel1, gridBagConstraints);

        jLabel2.setText("Lastname");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 10, 0, 0);
        add(jLabel2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.ipadx = 188;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 10, 0, 10);
        add(textfieldLastname, gridBagConstraints);

        buttonSave.setText("save");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 82, 11, 10);
        add(buttonSave, gridBagConstraints);

        buttonClose.setText("close");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 10, 11, 0);
        add(buttonClose, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.pennychecker.swing.presenter.ext.component.SButton buttonClose;
    private com.pennychecker.swing.presenter.ext.component.SButton buttonSave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private com.pennychecker.swing.presenter.ext.component.STextField textfieldFirstname;
    private com.pennychecker.swing.presenter.ext.component.STextField textfieldLastname;
    // End of variables declaration//GEN-END:variables

    public HasClickHandlers getSaveButton() {
        return buttonSave;
    }

    public HasClickHandlers getCloseButton() {
        return buttonClose;
    }

    public String getFirstname() {
        return textfieldFirstname.getText();
    }

    public String getLastName() {
        return textfieldLastname.getText();
    }

    public void setErrorMessage(String message) {
        JOptionPane.showConfirmDialog(this, message, "Error", 1);
    }

    public void clearForm() {
        textfieldFirstname.setText("");
        textfieldLastname.setText("");
    }

    public void setUser(User user) {
        textfieldFirstname.setText(user.getFirstname());
        textfieldLastname.setText(user.getLastname());
    }

    public JComponent asComponent() {
        return this;
    }
}
